/*
* Powered By [choudou5]
* Web Site: http://solr.javasaas.top
* License：MIT
* Since 2018 - 2020
*/
package com.choudou5.solr.service.sys;

import com.choudou5.solr.annotation.ServiceInfo;
import com.choudou5.solr.bean.common.SqlQueryBean;
import com.choudou5.solr.bean.sys.SysExceptionBean;
import com.choudou5.solr.service.BaseService;
import com.choudou5.solr.util.*;
import com.choudou5.solr.util.exception.BizException;

import java.util.List;


/**
 * @name 系统异常 Service
 * @author choudou5
 * @date：2018-08-25
 */
@ServiceInfo(table = "solr_sys_exception", desc = "系统异常")
public class SysExceptionService extends BaseService<SysExceptionBean> {

    @Override
    public List<SysExceptionBean> findList(SqlQueryBean sqlQueryBean) throws BizException {
        List<SysExceptionBean> list = super.findList(sqlQueryBean);
        if(CollUtil.isNotEmpty(list)){
            for (SysExceptionBean bean : list) {
                //解压内容
                bean.setException(GzipUtil.uncompress(bean.getException()));
            }
        }
        return list;
    }


    public void batchAddBean(List<SysExceptionBean> list) throws BizException {
        if(CollUtil.isNotEmpty(list)){
            int dex = 1;
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO " + getTable() + " (`id`, `module`, `type`,`brief`, `exception`, `access_ip`,  `server`, `create_time`) VALUES");
            for (SysExceptionBean bean : list) {
                sql.append((dex>1?", ":"") + "("+ SysSeqUtil.generateId()+", '"+bean.getModule()+"', '"+bean.getType()+"', '"+bean.getBrief()+"', '"+bean.getException()+"', '"+bean.getAccessIp()+"','"+bean.getServer()+"','"+ DateUtil.now()+"')");
                dex++;
            }
            if(dex > 1){
                LogDeBugUtil.debug(sql.toString());
                batchAdd(sql.toString());
            }
        }
    }

}